# !/usr/bin python3
# encoding: utf-8 -*-
# @file     : file_load.py
# @author   : 沙陌 Matongxue_2
# @Time     : 2023/5/14 14:30
# @Copyright: 北京码同学
import openpyxl
import yaml

from paths_manager import buyer_yaml, mtxshop_data_excel, common_yaml


def read_excel(filepath, sheet_name):
    '''
    :param filepath: Excel文件路劲
    :param sheet_name: 工作表名称
    :return:
    '''
    # 加载指定路径的Excel文件
    wb = openpyxl.load_workbook(filepath)
    sheet_data = wb[sheet_name]  # 获取某个sheet工作表的数据

    # print(sheet_data)
    lines_count = sheet_data.max_row  # 获取总行数
    cols_count = sheet_data.max_column  # 获取总列数

    # print(lines_count,cols_count)
    # 初始化数据列表
    data = []  # 用来存储所有行的数据，每行数据都是一个列表

    # 注意：openpyxl里读取时行号和列号都是从1开始
    for l in range(2, lines_count + 1):  # l:2,3,4,5,6,7
        line = []  # 用来存储当前行所有的单元格数据

        # 读取每一列的数据，然后存到列表中
        for c in range(1, cols_count + 1):  # c:1,2,3,4,5,6
            cell_data = sheet_data.cell(l, c).value
            # print(cell_data)
            # 如果不填，cell_data的值就是None
            if cell_data == None:
                cell_data = ''
            line.append(cell_data)
        data.append(line)
    return data


def load_yaml_file(filepath):
    with open(file=filepath, mode='r', encoding='UTF-8') as f:
        # f 是文件句柄
        content = yaml.load(f, Loader=yaml.FullLoader)
        return content  # 返回一个字典


def write_yaml(filepath, content):
    with open(file=filepath, mode='w', encoding='UTF-8') as f:
        # {pass: 1, fail: 2} -> pass: 1 fail: 2
        yaml.dump(content, f, Dumper=yaml.Dumper)


if __name__ == '__main__':
    # print(read_excel(mtxshop_data_excel,'添加收货地址'))
    h = load_yaml_file(common_yaml)
    print(h['buyerPwd'])
    print(h.get('buyerUsername'))
